﻿//edit by niko
window.IsValid = false;
function onblur_CheckNumber(o) {
    var _re = /^\d{1,10}$/gi;
    if (o.re) {
        if (o.re == 1) {
            _re = /^[1-9]\d{0,9}$/gi;
        } else {
            _re = o.re;
        }
    }
    var _note = typeof o.note == "string" ? $("#" + o.note) : o.note;
    var _value = _note.find("input").val();
    if (o.required && o.required == false && _value == "") {
        _note.removeClass("error");
        window.IsValid = true;
        return true;
    }
    if (_note && _re.test(_value)) {
        _note.removeClass("error");
        window.IsValid = true;
        return true;
    } else {
        _note.addClass("error");
        window.IsValid = false;
        return false;

    }
}

function onblur_CheckText(o) {
    var _re = null;
    if (o.re) {
        if (o.re == 1) {
            _re = /^[1-9]\d{0,9}$/gi;
        } else {
            _re = o.re;
        }
    }
    var _note = typeof o.note == "string" ? $("#" + o.note) : o.note;
    var _value = _note.find("input").val();
    if (o.required && o.required == false && _value == "") {
        _note.removeClass("error");
        window.IsValid = true;
        return true;
    }
    if (!_re) {
        if (o.required && o.required == true) {
            _note.addClass("error");
            window.IsValid = false;
            return false;
        } else {
            _note.removeClass("error");
            window.IsValid = true;
            return true;
        }
    } else {
        if (_note && _re.test(_value)) {
            _note.removeClass("error");
            window.IsValid = true;
            return true;
        } else {
            _note.addClass("error");
            window.IsValid = false;
            return false;
        }
    }
}

function AjaxDelete(op) {
    console.log(op);
    var senddata = {
        ID: op.id
    };
    $.post(op.url, senddata,
                function (data) {
                    console.log(data);
                    if (data.Success == true) {
                        $.globalMessenger().post(data.Message);
                    } else {
                        $.globalMessenger().post({
                            message: data.Message,
                            type: 'error',
                            showCloseButton: true
                        });
                    }
                }, "json");
}

function AjaxMulDelete(op) {
    console.log(op);
    var nodes = $("input[type='checkbox']:checked");
    if (nodes.length == 0) {
        $.globalMessenger().post({
            message: "没有任何单选框被选中",
            type: 'error',
            showCloseButton: true
        });
        return;
    }
    var _IDs = [];
    nodes.each(function () {
        _IDs.push(this.id);
    });
    var senddata = {
        IDs: _IDs.join(',')
    };
    $.post(op.url, senddata,
                function (data) {
                    console.log(data);
                    if (data.Success == true) {
                        $.globalMessenger().post(data.Message);
                    } else {
                        $.globalMessenger().post({
                            message: data.Message,
                            type: 'error',
                            showCloseButton: true
                        });
                    }
                }, "json");
}

$(function () {

    // Checkbox 全选 
    $("#CheckAll").click(function () {
        checkbox_CheckAll();
    });

});


function checkbox_CheckAll(e) {
    if ($(e).attr("checked") == "checked") { // 全选 
        $("input[type='checkbox']").each(function () {
            if ($(this).attr("disabled") != "disabled")
                $(this).attr("checked", true);
        });
    } else { // 取消全选 
        $("input[type='checkbox']").each(function () {
            if ($(this).attr("disabled") != "disabled")
                $(this).attr("checked", false);
        });
    }
}

function ErrorMsg(content, url) {
    $.globalMessenger().post({
        message: content || "出现错误,请联系管理员",
        type: 'error',
        actions: {
            succ: {
                label: '确定',
                action: function () {
                    window.location.href = url || "/";
                }
            },
            cancel: {
                label: '取消',
                action: function () {
                }
            }
        }
    });
}



function rate_tran1(value) {
    var ls = new Array("很差", "差", "一般", "好", "很好");
    return ls[value - 1];
}
function rate_tran2(value) {
    var ls = new Array("很低", "低", "一般", "良好", "高超");
    return ls[value - 1];
}
function rate_tran3(value) {
    var ls = new Array("坑爹", "差", "正常", "好", "很好");
    return ls[value - 1];
}
function rate_tran4(value) {
    var ls = new Array("煎熬", "不满", "一般", "满意", "享受");
    return ls[value - 1];
}
function rate_tran5(value) {
    var ls = new Array("我去", "哎", "一般", "不错", "光明");
    return ls[value - 1];
}
